<?php 

// require('lib/php_sam.php');
require("lib/phpMQTT.php");
date_default_timezone_set('PRC');
define('SAM_HOST', '6nww.com');
define('SAM_PORT', '1883');

$mqtt = new phpMQTT(SAM_HOST, SAM_PORT, "musikar");
 
if(!$mqtt->connect()){
    exit(1);
}
 
$topics['task'] = array("qos"=>0, "function"=>"procmsg");
// $topics['111'] = array("qos"=>0, "function"=>"procmsg");
// $topics='topic://hibike/lock/#';
$mqtt->subscribe($topics,0);
 
while($mqtt->proc()){
 
}

$mqtt->close();



function procmsg($topic='task',$msg){
    $message= "Msg Recieved: ".date("Y-m-d H:i:s")."\nTopic:{$topic}\n$msg\n";
    echo $message;
    errorLog($message,'subscribe.log');
}
//errorLog(G('begin','end',6),'time.log');
/**
 * 打印错误日志
 * 如果后缀为php的，需要删除后重新生成，其他后缀的，重复写入，手动删除日志
 * 
 */
function errorLog($message,$file)
{
   $log_dir=$_SERVER['DOCUMENT_ROOT']."/log/".date('Ymd')."/";
    //$log_dir=CACHE_ROOT."/log/".date('Y-m-d')."/";
    // $log_dir=dirname(__FILE__)."/";
    // echo $log_dir;die;
    if(!is_dir($log_dir)){
        @mkdir($log_dir,0777,true);
    }
    $file=$log_dir.$file;
    if(is_array($message)){
        $arr=explode(".",$file);
        if($arr[1]=='php'){
            error_log("<?php \n return ".var_export($message, true)."\n", 3,$file);
        }else{
             error_log(var_export($message, true)."\n", 3,$file);
        }
        
    }else{
       error_log($message."\n\n", 3,$file); 
    }
   // xdug($message);
    // error_log($message, 3,$file);
   
} 
?>